import config from "./config";
async function request(params = {method: "", data: '', url: '', noLoading: true, loadText: ''}) {
    console.log(getApp())
    if (!getApp().globalData.userInfo.token) {
        getApp().globalData.userInfo.token = await login();
    }
    // console.log(getApp().globalData.userInfo)
    if (!params.noLoading && params.loadText) {
        uni.showLoading({
            mask: true,
            title: params.loadText
        });
    }
    return new Promise((resolve, reject) => {
        //要做的事
        uni.request({
            url: params.noMock ? config.http_url + params.url : config.mock_url + params.url,
            // url: 'http://10.200.51.50:1888/api/emptech-download/mp-photo/' + params.url,
            // url: 'http://192.168.9.32:10079/emptech-download/mp-photo/' + params.url,
            method: params.method,
            data: params.data,
            timeout: 1000 * 60 * 5,
            header: {
                'content-type': 'application/json', //默认值
                'Authorization': 'Basic ' + getApp().globalData.userInfo.token
            },
            dataType: 'json',
            success: function (res) {
                if (params.loadText) {
                    uni.hideLoading();
                }
                if (res.data.code == 100) {
                    resolve(res.data);
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.data.msg,
                        duration: 3000
                    });
                    reject(res.data);
                }
            },
            fail: function (res) {
                console.log(123, res)
                // uni.hideLoading();
                uni.showToast({
                    icon: 'none',
                    title: '网络繁忙，请重试',
                    duration: 3000
                });
                reject(res);
            },
            complete: function (res) {

                console.log(res)
                // uni.hideLoading();
            }
        });
    });
}

function login() {
    uni.showLoading({
        title: '登录中',
        mask: true
    });
    var _this = this;
    return new Promise((resolve, reject) => {
        // #ifdef MP-ALIPAY
        my.getAuthCode({
            scopes: 'auth_user',
            success: (res) => {
                console.log(res)
                loginRequest(res.authCode).then(token => {
                    resolve(token)
                });
            },
            fail: (err) => {
                // console.log(err)
            }
        })
        // #endif

        // #ifdef MP-WEIXIN
                wx.login({
                    success(res) {
                        if (res.code) {
                            //发起网络请求
                            loginRequest(res.code).then(token => {
                                console.log(99, token)
                                resolve(token)
                            });
                            wx.getUserInfo({
                                success: (res1) => {
                                    let data = {
                                        authCode: res.code,
                                        ...res1
                                    }

                                },
                                fail: (err) => {
                                    console.log('失败', err);
                                }
                            })
                        } else {
                            console.log('登录失败！' + res.errMsg)
                        }
                    }
                })
        // #endif
    })
}

function loginRequest(code) {
    var platform = ''
    // #ifdef MP-ALIPAY
    platform = 'alipay'
    // #endif
    // #ifdef MP-WEIXIN
    platform = 'wx'
    // #endif

    return new Promise((resolve, reject) => {
        uni.request({
            url: config.http_url + '/api/v1/zgj/user/login',
            method: 'POST',
            data: {
                authCode: code,
                web_id: getApp().globalData.scene,
                platform: platform
            },
            headers: {
                'content-type': 'application/json', //默认值
                'headers': ''
            },
            dataType: 'json',
            success: function (res) {
                console.log(res)
                // wx.setStorage({
                //     key: "login",
                //     data: res.data.data
                // })
                uni.setStorageSync('login', res.data.data)
                if (res.data.data.token) {
                    // wx.setStorage({
                    //     key: "token",
                    //     data: res.data.data.token
                    // })
                    uni.setStorageSync('token', res.data.data.token)
                    resolve(res.data.data.token)
                } else {
                    uni.navigateTo({url: '/pages/index/authorization'});
                }
            },
            fail: function (res) {
                uni.showToast({
                    icon: 'none',
                    title: '服务器偷懒啦，请重试！',
                    duration: 3000
                });
            },
            complete: function (res) {
                uni.hideLoading();
            }
        });
    })

}

export default {
    request
}
